package com.manning.readinglist.controller;

import com.manning.readinglist.model.Book;
import com.manning.readinglist.dao.ReadingListRepositoryDao;
import com.manning.readinglist.service.ReadingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname ReadingListController
 * @Description TODO
 * @Date 2020/5/20 11:48
 * @Created by seed
 */
@Controller
@RequestMapping("/readingList")
public class ReadingListController {

    @Resource
    private ReadingListService readingListService;

    @RequestMapping(value = "{reader}",method = RequestMethod.GET)
    public String readers(@PathVariable("reader")String reader, Model model){
        List<Book> readingList = readingListService.findByReader(reader);
        if (readingList != null) {
            model.addAttribute("books", readingList);
        }

        return "readingList";
    }

    @RequestMapping(value="/{reader}", method=RequestMethod.POST)
    public String addToReadingList(
            @PathVariable("reader") String reader, Book book) {
        book.setReader(reader);
        readingListService.save(book);
        return "redirect:/readingList/{reader}";
    }

}
